package com.skyrin.draglayoutdemo;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

public class DragLinearLayout extends LinearLayout {
    /**
     * The list of {@link View}s that will be draggable.
     */
    private List<View> mDragViews;

    /**
     * The {@link DragLinearLayout.DragLinearLayoutController} that will be notify on drag.
     */
    private DragLinearLayout.DragLinearLayoutController mDragLinearLayoutController;

    private ViewDragHelper mDragHelper;

    public DragLinearLayout(Context context) {
        this(context,null,0,0);
    }

    public DragLinearLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0,0);
    }

    public DragLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr,0);
    }

    public DragLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mDragViews = new ArrayList<>();

        mDragHelper = ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback() {
            @Override
            public boolean tryCaptureView(View child, int pointerId) {
                return mDragViews.contains(child);
            }

            @Override
            public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
                mDragLinearLayoutController.onViewPositionChanged(changedView,left,top,dx,dy);
                super.onViewPositionChanged(changedView, left, top, dx, dy);
            }

            @Override
            public int clampViewPositionHorizontal(View child, int left, int dx) {
                return left;
            }

            @Override
            public int clampViewPositionVertical(View child, int top, int dy) {
                return top;
            }

            @Override
            public void onViewCaptured(View capturedChild, int activePointerId) {
                super.onViewCaptured(capturedChild, activePointerId);
                if (mDragLinearLayoutController != null) {
                    mDragLinearLayoutController.onDragDrop(capturedChild,true);
                }
            }

            @Override
            public void onViewReleased(View releasedChild, float xvel, float yvel) {
                super.onViewReleased(releasedChild, xvel, yvel);
                if (mDragLinearLayoutController != null) {
                    mDragLinearLayoutController.onDragDrop(releasedChild,false);
                }
            }
        });
    }

    @Override
    public boolean onInterceptHoverEvent(MotionEvent event) {
        final int action = event.getActionMasked();
        if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
            mDragHelper.cancel();
            return false;
        }
        return mDragHelper.shouldInterceptTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mDragHelper.processTouchEvent(event);
        return true;
    }

    /**
     * Adds a new {@link View} to the list of views that are draggable within the container.
     * @param dragView the {@link View} to make draggable
     */
    public void addDragView(View dragView) {
        mDragViews.add(dragView);
    }

    /**
     * Sets the {@link DragLinearLayout.DragLinearLayoutController} that will receive the drag events.
     * @param dragLinearLayoutController a {@link DragLinearLayout.DragLinearLayoutController}
     */
    public void setDragLinearController(DragLinearLayout.DragLinearLayoutController dragLinearLayoutController) {
        mDragLinearLayoutController = dragLinearLayoutController;
    }

    /**
     * A controller that will receive the drag events.
     */
    public interface DragLinearLayoutController {

        void onDragDrop(View view,boolean captured);

        void onViewPositionChanged(View changedView, int left, int top, int dx, int dy);
    }
}
